package com.suxin.test.netty.adv.client;

import lombok.extern.slf4j.Slf4j;
import org.example.netty.adv.vo.Message;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Author: tang
 * @Description:
 * @Date 2025/1/21 16:06
 */
@Slf4j
public class CilentBusiRespQueue {

    private static final Map<String, SynchronousQueue<Message>> dataMapQueue = new HashMap<>(4);

    public static Message poll(String id) {
        try {
            return dataMapQueue.get(id).poll(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public static void put(Message msg) {
        dataMapQueue.put(msg.getMessageHeader().getMsgId(), new SynchronousQueue<>());
    }

    public static void putResult(Message msg) {
        try {
            dataMapQueue.get(msg.getMessageHeader().getMsgId()).put(msg);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public static void remove(Message message) {
        dataMapQueue.remove(message.getMessageHeader().getMsgId());
    }
}
